package com.lazy.core.service.impl;

import com.github.pagehelper.PageHelper;
import com.lazy.core.entity.RunnerInfo;
import com.lazy.core.mapper.RunnerInfoMapper;
import com.lazy.core.service.RunnerInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Service
public class RunnerInfoServiceImpl implements RunnerInfoService {

    @Autowired
    RunnerInfoMapper runnerInfoMapper;
    @Autowired
    HttpServletResponse response;
    @Autowired
    @Qualifier(value="exportRunnerInfoService")
    ExportRunnerInfoServiceImpl exportRunnerInfoService;

    @Override
    public Integer saveOrUpdate(RunnerInfo runnerInfo) {
        int result = 0;
        if(null != runnerInfo.getId()){
            result = runnerInfoMapper.updateByPrimaryKeySelective(runnerInfo);
        }else{
            result = runnerInfoMapper.insertSelective(runnerInfo);
        }
        return result;
    }

    @Override
    public List<RunnerInfo> findByCondition(RunnerInfo runnerInfo, Integer pageNo, Integer pageSize) {
        if(null==pageNo){
            pageNo = 1;
        }
        if(null==pageSize){
            pageSize = 10;
        }
        PageHelper.startPage(pageNo, pageSize);
        List<RunnerInfo> runnerInfos = runnerInfoMapper.select(runnerInfo);
        return runnerInfos;
    }

    @Override
    public void exportRunnerInfo(RunnerInfo runnerInfo, Integer pageNo, Integer pageSize) throws IOException {
        if(null!=pageNo && null!=pageSize){
            PageHelper.startPage(pageNo, pageSize);
        }
        List<RunnerInfo> runnerInfos = runnerInfoMapper.select(runnerInfo);

        String[] title={"序号","姓名","性别","生日","手机号","邮箱","证件类型","证件号码","国籍","地址","紧急联系人","紧急联系人电话","血型","T恤尺码","最好成绩","跑者类型"};
        exportRunnerInfoService.exportExcel("信息一览", title, runnerInfos);
    }
}
